/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import model.Cookbook;
import model.Ingredient;
import model.Measure;
import model.Recipe;
import model.SearchList;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 *
 * @author zimi
 */
public class XmlDataProvider implements DataProvider {

    public static String path;

    public XmlDataProvider(String path) {
        this.path = path;
    }

    public static String getPathToPics() {
        String ret = path;
        ret = ret.substring(0, ret.lastIndexOf('/')+1);
        ret += "pics/";
        return ret;
    }

    public void parseActivity(KXmlParser parser, Recipe recipe) throws XmlPullParserException, IOException {
    }

    public Cookbook importXML() {// throws XmlPullParserException, IOException{
        try {
            Cookbook cookbook = new Cookbook();
            KXmlParser parser = new KXmlParser();
            //InputStream is = this.getClass().getResourceAsStream(path);
            FileConnection fc = (FileConnection) Connector.open(path);
            InputStream is = fc.openInputStream();
            parser.setInput(new InputStreamReader(is));
            parser.nextTag();
            parser.require(XmlPullParser.START_TAG, null, "vector");
            while (parser.nextTag() != XmlPullParser.END_TAG) {
                parser.require(XmlPullParser.START_TAG, null, null);
                cookbook.addRecipe(new Recipe(parser));
                parser.require(XmlPullParser.END_TAG, null, null);
            }
            parser.next();
            parser.require(XmlPullParser.END_DOCUMENT, null, null);
//            Vector recipes = (Vector) cookbook.listRecipes();
            SearchList recipes = (SearchList) cookbook.listRecipes();
            Enumeration rEnum = recipes.elements();

//            while(rEnum.hasMoreElements()){
//                Recipe recipe = (Recipe) rEnum.nextElement();

            for(int i=0;i<recipes.size();i++){
                Recipe recipe = (Recipe) recipes.elementAt(i);

                Vector ingredients = recipe.listIngredients();
                Enumeration iEnum = ingredients.elements();
                while (iEnum.hasMoreElements()) {
                    Ingredient ingredient = (Ingredient) iEnum.nextElement();
                    cookbook.addPossibleProduct(ingredient.getProduct());
                }
              //  cookbook.addPossibleCategory(recipe.getCategory());
            }

            cookbook.addPossibleMeasure(Measure.getGram());
            cookbook.addPossibleMeasure(Measure.getKilogram());
            cookbook.addPossibleMeasure(Measure.getLitre());
            cookbook.addPossibleMeasure(Measure.getMililitre());
            cookbook.addPossibleMeasure(Measure.getNoUnit());
            cookbook.addPossibleMeasure(Measure.getUnit());

            return cookbook;
        } catch (XmlPullParserException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public boolean exportXML(Cookbook cookbook) {
        //throw new UnsupportedOperationException("todo");
        return false;
    }
}
